/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package org.myontology.controller.impl;

import model.Vocabulary;

import org.myontology.controller.AbstractAction;
import org.myontology.controller.AttributeHolder;
import org.myontology.controller.Mailer;
import org.myontology.init.Globals;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.repository.RepositoryConnection;

import controller.sesameDataConnector.AbstractDataConnector;

/**
 * Action implementation class for Servlet: CreateAdmin
 *
 */
 public class CreateAdmin extends AbstractAction {
	
	
	private Mailer mailer;
	

	public void execute(AttributeHolder attributes) throws Exception {
		
		try {
			
			// delete an existing admin first (to reset the default values)
			RepositoryConnection con = AbstractDataConnector.getConnection();
			con.remove( new URIImpl(Vocabulary.USER_NAMESPACE + "admin" ), null, null );
			con.commit();
			
			
			String adminMail = Globals.adminMail.getValue(attributes);
			
			model.dataContainer.UserContainer cont = new model.dataContainer.UserContainer();
			
			cont.setUserid( "admin" );
			cont.setFirst_name( "a" );
			cont.setLast_name( "a" );
			cont.setE_mail( adminMail );
			cont.setAddress( "-" );
			String passwd = ""+Math.abs((System.currentTimeMillis()+""+Math.random()).hashCode());
			cont.setPwd( passwd );
			controller.sesameDataConnector.UserConnector.insert(cont);
			
			
			mailer.mail(adminMail, "Your new admin password", passwd, attributes);
			
		} catch (Exception e){
			
			
		}		

		
	}   	  	    
}
